﻿using Newtonsoft.Json.Linq;
using Wicture.DbRESTFul.SQL;

namespace Wicture.EPC.Service.Etka.Repository
{
    public class PreprocessRepository : DbRESTFulRepository, IParameterPreprocessor
    {
        [PreprocessMethod]
        public JToken SyncDb(JToken param, DbRESTFulRepository repository)
        {
            return param;
        }
        //[PreprocessMethod]
        //public JToken SyncDb(JToken param, DbRESTFulRepository repository, CSIItem codeItem)
        //{
        //if (!ConfigurationManager.Settings.Document.ContainsKey("DbSections"))
        //{
        //    return param;
        //}
        //JToken dbSections = ConfigurationManager.Settings.Document.GetValue("DbSections");
        //var connArray = JArray.FromObject(dbSections);
        //if (connArray == null || connArray.Count == 0)
        //{
        //    return param;
        //}
        //List<string> connStrList = new List<string>();
        //MySqlConnectionObject currentConnObject = new MySqlConnectionObject(repository.ConnectionManager.WriteConnectionString);
        //Task.Run(() =>
        //{
        //    foreach (var connToken in connArray)
        //    {
        //        var conn = connToken.ToString();
        //        var connObject = new MySqlConnectionObject(conn);
        //        if (connObject.EqualInstance(currentConnObject))
        //        {
        //            continue;
        //        }
        //        try
        //        {
        //            using (var mysqlConn = new MySqlConnection(conn))
        //            {
        //                if (mysqlConn.State != ConnectionState.Open)
        //                {
        //                    mysqlConn.Open();
        //                }
        //                mysqlConn.ChangeDatabase(currentConnObject.Database);
        //                var sql = codeItem.code.ToString();
        //                var paramObj = DynamicParametersBuilder.Build(param);
        //                mysqlConn.Execute(sql, paramObj);
        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //            LoggerManager.Logger.LogError(ex, $"Sync datebase error:Server:{connObject.Ip}:{connObject.Port},Database:{currentConnObject.Database}");
        //        }
        //    }
        //});
        //return param;
        //}
    }
}
